﻿var DDLMessage = 'Select valid ';
var TxBoxMessage = " cannot be left blank."
var ErrorColor = '#FFFF90';
var DefaultColor = '#FFFFFF';
var JavaScriptError = 'Please correct the cells which are highlighted.';
var InvaliEmail = "Invalid Email Id.";

//#######################################################################################//
//This Function is being used for displaying error with respect of any control.          //
//#######################################################################################//
function DisplayError(objCtrl, Message, FieldName, objErrorCounter) {
    objCtrl.title = FieldName + Message;
    objCtrl.style.backgroundColor = ErrorColor;
    objErrorCounter.value = (parseInt(objErrorCounter.value) + 1).toString();
}

function DisplayErrorFromCS(CtrlCliendID, Message, FieldName) {
    objCtrl = document.getElementById(CtrlCliendID);
    objCtrl.title = FieldName + Message;
    objCtrl.style.backgroundColor = ErrorColor;
    alert(JavaScriptError);
    return false;
}

function DisplayErrorFromCSWithoutAlert(CtrlCliendID, Message, FieldName) {
    objCtrl = document.getElementById(CtrlCliendID);
    objCtrl.title = FieldName + Message;
    objCtrl.style.backgroundColor = ErrorColor;
}
//#######################################################################################//
//This Function is being used for Removing error with respect of any control.            //
//#######################################################################################//
function RemoveError(objCtrl) {
    if (objCtrl != null) {
        objCtrl.title = "";
        objCtrl.style.backgroundColor = DefaultColor;
    }

}


//#######################################################################################//
//This Function is being used for Trimming the string value.                              //
//#######################################################################################//
function FasterTrim(str) {
    var str = str.replace(/^\s+/, "").replace(/\s+$/, "");
    return str;
}


//#######################################################################################//
//This Function is being used for change the character in Upper case.                    //
//#######################################################################################//
function ChangeUpper(obj) {
    obj.value = obj.value.toUpperCase();
}


//#######################################################################################//
//This Function is being used for Mandatory condition For Text Box.                      //
//#######################################################################################//
function IsTextBoxEmpty(objTxtBox, FieldName, objErrorCounter) {
    if (FasterTrim(objTxtBox.value) != '') {
        return false;
    }
    else {
        DisplayError(objTxtBox, TxBoxMessage, FieldName, objErrorCounter);
        return true;
    }
}

//#######################################################################################//
//This Function is being used to know given object contain pure number or not.           //
//#######################################################################################//
function isNumber(objTxtBox) {
    var str = FasterTrim(objTxtBox.value);
    var i;
    for (i = 0; i < str.length; i++) {
        // Check that current character is number.
        var c = str.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

//#######################################################################################//
//Function to clear textbox on Focus                                                     //
//#######################################################################################//

function onFocusClear(obj, text) {
    if (obj.value == text) {
        obj.value = '';
    }
}

//#######################################################################################//
//Function to Validate Company Master                                                //
//#######################################################################################//

function ValidateCompanyMaster(txtFirstNameClientID, txtLastNameClientID,
                            txtContactNumber1ClientID, txtEmailIDClientID,
                            txtConfirmEmailIDClientID, txtPasswordClientID,
                            txtConfirmaPasswordClientID, ddlStateClientID,
                            ddlCityClientID, hdnAlertCounterClientID) {
    txtFirstNameClientID = document.getElementById(txtFirstNameClientID);
    txtLastNameClientID = document.getElementById(txtLastNameClientID);
    txtContactNumber1ClientID = document.getElementById(txtContactNumber1ClientID);
    txtEmailIDClientID = document.getElementById(txtEmailIDClientID);
    txtConfirmEmailIDClientID = document.getElementById(txtConfirmEmailIDClientID);
    txtPasswordClientID = document.getElementById(txtPasswordClientID);
    txtConfirmaPasswordClientID = document.getElementById(txtConfirmaPasswordClientID);
    ddlStateClientID = document.getElementById(ddlStateClientID);
    ddlCityClientID = document.getElementById(ddlCityClientID);
    hdnAlertCounterClientID = document.getElementById(hdnAlertCounterClientID);

    hdnAlertCounterClientID.value = "0";

    RemoveAllErrors(txtFirstNameClientID, txtLastNameClientID,
                            txtContactNumber1ClientID, txtEmailIDClientID,
                            txtConfirmEmailIDClientID, txtPasswordClientID,
                            txtConfirmaPasswordClientID,
                            ddlStateClientID,
                            ddlCityClientID,
                            hdnAlertCounterClientID);

    if (trim(txtFirstNameClientID.value) == "" || trim(txtFirstNameClientID.value)=='First Name') {
        DisplayError(txtFirstNameClientID, 'First Name', '', hdnAlertCounterClientID)
        txtFirstNameClientID.focus();

    }
    if (trim(txtLastNameClientID.value) == "" || trim(txtLastNameClientID.value)=='Last Name') {
        DisplayError(txtLastNameClientID, 'Last Name', '', hdnAlertCounterClientID)
        txtLastNameClientID.focus();

    }
    if (trim(ddlStateClientID.value) == "0" || trim(ddlStateClientID.value) =='Country') {
        DisplayError(ddlStateClientID, 'Select State', '', hdnAlertCounterClientID)
        ddlStateClientID.focus();

    }
    if (trim(ddlCityClientID.value) == "0" || trim(ddlCityClientID.value) =='City') {
        DisplayError(ddlCityClientID, 'City.', '', hdnAlertCounterClientID)
        ddlCityClientID.focus();

    }
    if (trim(txtContactNumber1ClientID.value) == "" || trim(txtContactNumber1ClientID.value)=='Mobile[1]') {
        DisplayError(txtContactNumber1ClientID, 'Mobile[1]', '', hdnAlertCounterClientID)
        txtContactNumber1ClientID.focus();

    }
    if (trim(txtEmailIDClientID.value) == "" || trim(txtEmailIDClientID.value)=='Your Email') {
        DisplayError(txtEmailIDClientID, 'Please enter email id.', '', hdnAlertCounterClientID)
        txtEmailIDClientID.focus();

    }
    if (trim(txtConfirmEmailIDClientID.value) == "" || trim(txtConfirmEmailIDClientID.value)=='Confirm Email') {
        DisplayError(txtConfirmEmailIDClientID, 'Please enter confirm email id.', '', hdnAlertCounterClientID)
        txtConfirmEmailIDClientID.focus();

    }
    if (trim(txtEmailIDClientID.value) != trim(txtConfirmEmailIDClientID.value)) {
        DisplayError(txtConfirmEmailIDClientID, 'Please enter valid & same Email ID.', '', hdnAlertCounterClientID)
        txtConfirmEmailIDClientID.focus();
    }
    if (trim(txtPasswordClientID.value) == "" || trim(txtPasswordClientID.value) == "Password") {
        DisplayError(txtPasswordClientID, 'Please enter  password.', '', hdnAlertCounterClientID)
        txtPasswordClientID.focus();

    }
    if (trim(txtConfirmaPasswordClientID.value) == "" || trim(txtConfirmaPasswordClientID.value) == "Confirm Password") {
        DisplayError(txtConfirmaPasswordClientID, 'Please enter  password.', '', hdnAlertCounterClientID)
        txtConfirmaPasswordClientID.focus();

    }
   if (trim(txtPasswordClientID.value) != trim(txtConfirmaPasswordClientID.value)) {
        DisplayError(txtConfirmaPasswordClientID, 'Please enter valid & same password.', '', hdnAlertCounterClientID)
        txtConfirmaPasswordClientID.focus();
    }
   if (!IsTextBoxEmpty(txtEmailIDClientID, 'Email', hdnAlertCounterClientID)) {
       if (!IsValidEmail(txtEmailIDClientID, hdnAlertCounterClientID))
           DisplayError(txtEmailIDClientID, 'Please enter valid e-mail id.', '', hdnAlertCounterClientID)

   } if (!IsTextBoxEmpty(txtConfirmEmailIDClientID, 'Email', hdnAlertCounterClientID)) {
       if (!IsValidEmail(txtConfirmEmailIDClientID, hdnAlertCounterClientID))
           DisplayError(txtConfirmEmailIDClientID, 'Please enter valid e-mail id.', '', hdnAlertCounterClientID)

   }
    if (parseInt(hdnAlertCounterClientID.value) > 0) {
        alert(JavaScriptError);
        return false;
    }
}


function RemoveAllErrors(txtFirstNameClientID, txtLastNameClientID,
                            txtContactNumber1ClientID, txtEmailIDClientID,
                            txtConfirmEmailIDClientID, txtPasswordClientID,
                            txtConfirmaPasswordClientID,ddlStateClientID,
                            ddlCityClientID
                            ) {

    RemoveError(txtFirstNameClientID);
    RemoveError(txtLastNameClientID);
    RemoveError(txtContactNumber1ClientID);
    RemoveError(txtPasswordClientID);
    RemoveError(txtConfirmEmailIDClientID);
    RemoveError(txtConfirmaPasswordClientID);
    RemoveError(ddlStateClientID);
    RemoveError(ddlCityClientID);
}

function ValidatePostCompnayProduct(ttxtlotareaClientID, txtPriceM2ClientID,
                            txtYearClientID, up1ClientID,
                           ddlCountryClientID,ddlCityClientID, hdnAlertCounterClientID) {
    ttxtlotareaClientID = document.getElementById(ttxtlotareaClientID);
    txtPriceM2ClientID = document.getElementById(txtPriceM2ClientID);
    txtYearClientID = document.getElementById(txtYearClientID);
    up1ClientID = document.getElementById(up1ClientID);
    ddlCountryClientID = document.getElementById(ddlCountryClientID);
    ddlCityClientID = document.getElementById(ddlCityClientID);
     hdnAlertCounterClientID = document.getElementById(hdnAlertCounterClientID);
    hdnAlertCounterClientID.value = "0";

    RemoveAllErrors(ttxtlotareaClientID, txtPriceM2ClientID,
                            txtYearClientID, up1ClientID,
                            ddlCountryClientID, ddlCityClientID,                           
                            hdnAlertCounterClientID);

    if (trim(ttxtlotareaClientID.value) == "" || trim(ttxtlotareaClientID.value) == 'Product/Project Name') {
        DisplayError(ttxtlotareaClientID, 'Product/Project Name', '', hdnAlertCounterClientID)
        ttxtlotareaClientID.focus();

    }
    if (trim(txtYearClientID.value) == "" || trim(txtYearClientID.value) == 'Year') {
        DisplayError(txtYearClientID, 'Year', '', hdnAlertCounterClientID)
        txtYearClientID.focus();

    }
    if (trim(ddlCountryClientID.value) == "0" || trim(ddlCountryClientID.value) == 'Country') {
        DisplayError(ddlCountryClientID, 'Select Country', '', hdnAlertCounterClientID)
        ddlCountryClientID.focus();

    }
    if (trim(ddlCityClientID.value) == "0" || trim(ddlCityClientID.value) == 'City') {
        DisplayError(ddlCityClientID, 'City.', '', hdnAlertCounterClientID)
        ddlCityClientID.focus();

    }
    if (trim(txtPriceM2ClientID.value) == "" || trim(txtPriceM2ClientID.value) == 'Currency') {
        DisplayError(txtPriceM2ClientID, 'Currency', '', hdnAlertCounterClientID)
        txtPriceM2ClientID.focus();

    }
    if (trim(up1ClientID.value) == "") {
        DisplayError(up1ClientID, 'Please upload image.', '', hdnAlertCounterClientID)
        up1ClientID.focus();

    }    
    if (parseInt(hdnAlertCounterClientID.value) > 0) {
        alert(JavaScriptError);
        return false;
    }
}


function RemoveAllErrors(ttxtlotareaClientID, txtPriceM2ClientID,
                            txtYearClientID, up1ClientID,
                            ddlCountryClientID, ddlCityClientID
                            
                            ) {

    RemoveError(ttxtlotareaClientID);
    RemoveError(txtPriceM2ClientID);
    RemoveError(txtYearClientID);   
    RemoveError(up1ClientID);
    RemoveError(ddlCountryClientID);
    RemoveError(ddlCityClientID);
}


//#######################################################################################//
//This Function is being used to validate Email-ID.                                      //
//#######################################################################################//
function IsValidEmail(txtContactEmailIdClientID, hdnAlertCounterClientID) {

    //    txtEmailIdClientID = document.getElementById(txtEmailIdClientID);

    //    hdnAlertCounterClientID = document.getElementById(hdnAlertCounterClientID);



    var str = FasterTrim(txtContactEmailIdClientID.value);
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)


    if (str.indexOf(at) == -1) {
        DisplayError(txtContactEmailIdClientID, "", InvaliEmail, hdnAlertCounterClientID);
        return false;
    }
    else if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        DisplayError(txtContactEmailIdClientID, "", InvaliEmail, hdnAlertCounterClientID);
        return false;
    }
    else if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        DisplayError(txtContactEmailIdClientID, "", InvaliEmail, hdnAlertCounterClientID);
        return false;
    }
    else if (str.indexOf(at, (lat + 1)) != -1) {
        DisplayError(txtContactEmailIdClientID, "", InvaliEmail, hdnAlertCounterClientID);
        return false;
    }
    else if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        DisplayError(txtContactEmailIdClientID, "", InvaliEmail, hdnAlertCounterClientID);
        return false;
    }
    else if (str.indexOf(dot, (lat + 2)) == -1) {
        DisplayError(txtContactEmailIdClientID, "", InvaliEmail, hdnAlertCounterClientID);
        return false;
    }
    else if (str.indexOf(" ") != -1) {
        DisplayError(txtContactEmailIdClientID, "", InvaliEmail, hdnAlertCounterClientID);
        return false;
    }
    else if (str.indexOf(dot) + 1 == str.length) {
        DisplayError(txtContactEmailIdClientID, "", InvaliEmail, hdnAlertCounterClientID);
        return false;
    }
    else if (str.indexOf(dot) + 2 == str.length) {
        DisplayError(txtContactEmailIdClientID, "", InvaliEmail, hdnAlertCounterClientID);
        return false;
    }
    else {
        return true;
    }
}

